package com.lcf.app.quartz.common;

import com.lcf.app.quartz.job.MyJob;
import org.quartz.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author ChenFei
 * @date 2022/1/28
 */
@Component
public class QuartzUtil {

    private static final String DEFAULT_JOB_GROUP = "DEFAULT_JOB_GROUP";
    private static final String DEFAULT_TRIGGER_GROUP = "DEFAULT_TRIGGER_GROUP";
    private static final String JOB_DATA_NAME = "JOB_DATA_NAME";

    @Resource
    private Scheduler scheduler;

    /**
     * 添加并启动一个任务
     *
     * @param triggerName
     * @param jobName
     * @param cron
     * @param jobData
     */
    public void addTaskAndStart(String triggerName, String jobName, String cron, String jobData) throws SchedulerException {
        Trigger trigger = getTrigger(triggerName, cron);
        JobDetail jobDetail = getJobDetail(jobName, jobData);
        scheduler.scheduleJob(jobDetail, trigger);
    }

    /**
     * 清除(删除!)所有调度数据-所有作业，触发器日历
     */
    public void clear() throws SchedulerException {
        scheduler.clear();
    }

    /**
     * 暂停所有触发器
     *
     * @return
     */
    public void pauseAll() throws SchedulerException {
        scheduler.pauseAll();
    }

    /**
     * 暂停一个触发器
     */
    public void pauseTrigger(String triggerName) throws SchedulerException {
        scheduler.pauseTrigger(TriggerKey.triggerKey(triggerName, DEFAULT_TRIGGER_GROUP));
    }

    /**
     * 暂停一个任务,请使用：pauseTrigger
     */
    @Deprecated
    public void pauseJob(String jobName) throws SchedulerException {
        scheduler.pauseJob(JobKey.jobKey(jobName, DEFAULT_JOB_GROUP));
    }

    /**
     * 恢复(取消暂停)所有触发器
     */
    public void resumeAll() throws SchedulerException {
        scheduler.resumeAll();
    }

    /**
     * 恢复触发器
     */
    public void resumeTrigger(String triggerName) throws SchedulerException {
        scheduler.resumeTrigger(TriggerKey.triggerKey(triggerName, DEFAULT_TRIGGER_GROUP));
    }

    /**
     * 恢复任务：请使用resumeTrigger
     */
    @Deprecated
    public void resumeJob(String jobName) throws SchedulerException {
        scheduler.resumeJob(JobKey.jobKey(jobName, DEFAULT_JOB_GROUP));
    }

    /**
     * 从调度器和任何关联的触发器中删除指定的作业。
     */
    public void deleteJob(String jobName) throws SchedulerException {
        scheduler.deleteJob(JobKey.jobKey(jobName, DEFAULT_JOB_GROUP));
    }

    /**
     * 获取触发器
     *
     * @param triggerName
     * @param cron
     * @return
     */
    public Trigger getTrigger(String triggerName, String cron) {
        return TriggerBuilder
                .newTrigger()
                .withIdentity(triggerName, DEFAULT_TRIGGER_GROUP)
                .withSchedule(CronScheduleBuilder.cronSchedule(cron)).build();
    }

    /**
     * 获取一个具体任务
     *
     * @param jobName
     * @param jobData
     * @return
     */
    public JobDetail getJobDetail(String jobName, String jobData) {
        return JobBuilder
                .newJob(MyJob.class)
                .withIdentity(jobName, DEFAULT_JOB_GROUP)
                .usingJobData(JOB_DATA_NAME, jobData).build();
    }

}
